﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Common.XmlConfig.WebHelper
{
    public class SettingControl : WebControl
    {
        /// <summary>
        /// 配置信息
        /// </summary>
        public XmlConfig.ConfigSetting ConfigSetting { get; set; }
        public string OnClientClick { get; set; }
        public Unit Width_Title
        {
            get { return _Width_Title; }
            set { _Width_Title = value; }
        }
        public string GetValue()
        {
            switch (ConfigSetting.Type)
            {
                case SettingType.Bool:
                    return GetValue_Bool().ToString();
            }
            throw new Exception("控件类型错误！");
        }

        private string VALUECTRID = "CTR_VALUE";
        private Unit _Width_Title = new Unit("200px");

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            Panel Container = new Panel();
            switch (ConfigSetting.Type)
            {
                case SettingType.Bool:
                    Render_Bool(Container);
                    break;
                case SettingType.Date:
                    Render_Text(Container);
                    break;
                case SettingType.Default:
                    Render_Text(Container);
                    break;
                case SettingType.Email:
                    Render_Text(Container);
                    break;
                case SettingType.File:
                    Render_Text(Container);
                    break;
                case SettingType.Flash:
                    Render_Text(Container);
                    break;
                case SettingType.Html:
                    Render_Text(Container);
                    break;
                case SettingType.Image:
                    Render_Text(Container);
                    break;
                case SettingType.MultText:
                    Render_Text(Container);
                    break;
                case SettingType.Number:
                    Render_Text(Container);
                    break;
                case SettingType.Password:
                    Render_Text(Container);
                    break;
                case SettingType.Range:
                    Render_Text(Container);
                    break;
                case SettingType.Select:
                    Render_Text(Container);
                    break;
                case SettingType.Text:
                    Render_Text(Container);
                    break;
                case SettingType.Url:
                    Render_Text(Container);
                    break;
                case SettingType.Video:
                    Render_Text(Container);
                    break;
            }
            this.Controls.Add(Container);
            base.Render(writer);
        }
        protected void Render_Bool(Panel panl)
        {
            Label labText = new Label();
            labText.Width = Width_Title;
            labText.Text = ConfigSetting.Text;
            panl.Controls.Add(labText);
            CheckBox ckb = new CheckBox();
            ckb.ID = VALUECTRID;
            ckb.Checked = Convert.ToBoolean(ConfigSetting.Value);
            panl.Controls.Add(ckb);
        }
        protected void Render_Text(Panel panl)
        {
            Table table = new Table();
            table.Width = Width;
            TableRow row = new TableRow();
            TableCell tdlab = new TableCell();
            tdlab.Width = Width_Title;
            tdlab.Text = ConfigSetting.Text;
            TableCell tdtxt = new TableCell();
            TextBox txt = new TextBox();
            txt.Text = ConfigSetting.Value;
            txt.Width = new Unit("100%");
            tdtxt.Controls.Add(txt);
            row.Controls.Add(tdlab);
            row.Controls.Add(tdtxt);
            table.Controls.Add(row);
            panl.Controls.Add(table);
        }
        protected bool GetValue_Bool()
        {
            var ctr = this.FindControl(VALUECTRID) as CheckBox;
            return ctr.Checked;
        }
    }
}
